package com.uuah.server.context.notification;

import com.uuah.server.api.transport.IConnectable;

/**
 * Is fired by a connector when a connection is made or disconnected. A
 * disconnection can be caused by network failure, JMX, or the server shutting
 * down.
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:28:43
 * @version 1.0.0
 */
public class ConnectionNotification extends ServerNotification {
	private static final long serialVersionUID = -6455441938378523145L;
	public static final int CONNECTION_CONNECTED = CONNECTION_EVENT_ACTION_START_RANGE + 1;
	public static final int CONNECTION_FAILED = CONNECTION_EVENT_ACTION_START_RANGE + 2;
	public static final int CONNECTION_DISCONNECTED = CONNECTION_EVENT_ACTION_START_RANGE + 3;

	static {
		registerAction("connected", CONNECTION_CONNECTED);
		registerAction("connect failed", CONNECTION_FAILED);
		registerAction("disconnected", CONNECTION_DISCONNECTED);
	}

	public ConnectionNotification(IConnectable resource, String identifier,
			int action) {
		super(resource, action);
		resourceIdentifier = identifier;
	}

	protected String getPayloadToString() {
		if (source instanceof IConnectable) {
			return ((IConnectable) source).getConnectionDescription();
		}
		return source.toString();
	}

	public String getType() {
		if (action == CONNECTION_DISCONNECTED) {
			return TYPE_WARNING;
		}
		if (action == CONNECTION_FAILED) {
			return TYPE_ERROR;
		}
		return TYPE_INFO;
	}

}
